<?php
/**
 * Created by: i@fedax.cn
 * Date: 2018/7/16 22:39
 */

namespace app\admin\controller;

use app\admin\model\GiftFrag;
use app\admin\model\GoldEgg as GoldEggModel;
use app\admin\model\GoldEggLog as GoldEggLogModel;
use app\admin\model\Gift as GiftModel;
use app\admin\model\Vehicle as VehicleModel;
use app\admin\model\ActivityBoxLog as ActivityBoxLogModel;
use app\admin\model\ActivityBoxPrize as ActivityBoxPrizeModel;
/**
 * 砸金蛋活动
 * Class GoldEgg
 * @package app\admin\controller
 */
class GoldEgg extends Index
{
    /**
     * @package 金蛋类别
     * @return \think\response\View
     */
    public function index()
    {
        return view();
    }

    /**
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function indexData()
    {
        $goldEggModel = new GoldEggModel();

        $data = $goldEggModel->getDataList();
       // halt($data);
        return json($data);
    }

    /**
     * 添加宝箱类别
     * @return \think\response\Redirect
     */
    public function save()
    {
        if (request()->isPost()) {
            $goldEggModel = new GoldEggModel();
            $newData = array();
            $newData['egg_name'] = input('egg_name');
            $newData['egg_price'] = input('egg_price');
            $newData['ten_egg_price'] = input('ten_egg_price');
            $newData['discount'] = input('discount');
            try {
                $goldEggModel->save($newData);
            } catch (\Exception $e) {
                $this->error(lang('add') . lang('false') . $e->getMessage());
            }
            $this->success(lang('add') . lang('success'));
        }
        return redirect('/Miss');
    }

    /**
     * 删除
     * @return \think\response\Redirect
     */
    public function delete()
    {
        if (request()->isPost()) {
            $goldEggModel = new GoldEggModel();
            try {
                $goldEggModel->where('id', input('id'))->delete();
            } catch (\Exception $e) {
                $this->error(lang('delete') . lang('false') . $e->getMessage());
            }
            $this->success(lang('delete') . lang('success'));
        }
        return redirect('/Miss');
    }


    public function edit()
    {
        $id = input('id');
        $goldEggModel = new GoldEggModel();
        $goldInfo = $goldEggModel->where(['id'=>$id])->find();
        $goldInfo['prize_pool']  = unserialize( $goldInfo['prize_pool'] );
        return view()->assign('goldInfo',$goldInfo);
    }
    public function editData()
    {
        $data = input('post.');
        $goldEggModel = new GoldEggModel();

        $goldInfo = $goldEggModel->where('id',$data['id'])->find();
        if($data['start'] != $goldInfo['start']) dailyLog(['content'=>'修改了气球'.$data['id'].'的开始时间','type'=>3]);
        if($data['end'] != $goldInfo['end']) dailyLog(['content'=>'修改了气球'.$data['id'].'的结束时间','type'=>3]);
        if($data['mode_type'] != $goldInfo['mode_type'])
            dailyLog(['content'=>'修改了气球'.$data['id'].'的模式为'.$data['mode_type'],'type'=>3]);
        $prize_pool = unserialize($goldInfo['prize_pool']);
        if($prize_pool[1] != $data['one_pool'])
            dailyLog(['content'=>'修改了气球'.$data['id'].'的模式'.$data['mode_type'].'的奖池概率为'.$data['one_pool'],'type'=>3]);
        if($prize_pool[2] != $data['two_pool'])
            dailyLog(['content'=>'修改了气球'.$data['id'].'的模式'.$data['mode_type'].'的奖池概率为'.$data['two_pool'],'type'=>3]);

        $prize_pool = [1=>$data['one_pool'],2=>$data['two_pool']];
        $data['prize_pool'] = serialize($prize_pool);
        $res= $goldEggModel->save($data,['id' => $data['id']]);
        if($res) return ['code'=>100,'msg'=>'修改成功'];
        else return ['code'=>-100,'msg'=>'修改失败'];
    }

    /**
     * @package 金蛋抽奖记录
     * @return \think\response\View
     */
    public function logIndex()
    {
        return view();
    }

    /**
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function logIndexData()
    {
        $goldEggModel = new GoldEggLogModel();
        $page_config = array(
            'page' => input('offset', 1),
            'list_rows' => input('limit', 10),
        );
        $where = array();
        if (input('value')) {
            $where[] = [input('key'), '=', input('value')];
        }
        if (input('start_time')) {
            $where[] = ['add_time', '>', strtotime(input('start_time'))];
        }
        if (input('end_time')) {
            $where[] = ['add_time', '<', strtotime(input('end_time'))];
        }
        $data = $goldEggModel->getDataList($page_config, $where);
        return json($data);
    }

    /**
     * @package 金蛋碎片
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function fragIndex()
    {

        $page_config = array(
            'page' => input('offset', 1),
            'list_rows' => input('limit', 100),
        );
        $giftModel = new GiftModel();
        $map = array();
        $gift_list = $giftModel->getDataList($page_config, $map);
        $this->assign('gift_list', $gift_list);
        return view();
    }

    /**
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function fragIndexData()
    {
        $giftFragModel = new GiftFrag();
        $page_config = array(
            'page' => input('offset', 1),
            'list_rows' => input('limit', 10),
        );
        $data = $giftFragModel->getDataList($page_config);
        return json($data);
    }

    /**
     * 添加碎片奖品
     * @return \think\response\Redirect
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function fragSave()
    {
        if (request()->isPost()) {
            $giftFragModel = new GiftFrag();
            $newData = array();
            $newData['gift_id'] = input('gift_id');
            $newData['fragments_num'] = input('fragments_num');

            $rs = $giftFragModel->where('gift_id', input('gift_id'))
                ->find();
            if ($rs) {
                $this->error(lang('add') . lang('false') . '数据重复');
            }
            try {
                $giftFragModel->save($newData);
            } catch (\Exception $e) {
                $this->error(lang('add') . lang('false') . $e->getMessage());
            }
            $this->success(lang('add') . lang('success'));
        }
        return redirect('/Miss');
    }

    /**
     * 修改奖品
     * @return \think\response\Redirect
     */
    public function fragEdit()
    {
        if (request()->isPost()) {
            $giftFragModel = new GiftFrag();

            $newData = array();
            $newData['fragments_num'] = input('fragments_num');
            try {
                $giftFragModel->save($newData, ['id' => input('id')]);
            } catch (\Exception $e) {
                $this->error(lang('edit') . lang('false') . $e->getMessage());
            }
            $this->success(lang('edit') . lang('success'));
        }
        return redirect('/Miss');
    }

    /**
     * @return \think\response\Redirect
     */
    public function changeFragStatus()
    {
        if (request()->isPost()) {
            $giftFragModel = new GiftFrag();
            $newData = array();
            try {
                $giftFragDetail = $giftFragModel->where('id', input('id'))->find();
                $newData['status'] = $giftFragDetail['status'] === 1 ? 0 : 1;
                $giftFragModel->save($newData, ['id' => input('id')]);
            } catch (\Exception $e) {
                $this->error(lang('edit') . lang('false') . $e->getMessage());
            }
            $this->success(lang('edit') . lang('success'));
        }
        return redirect('/Miss');
    }


    /**
     * @return \think\response\View
     * @user 帅帅吴
     * @date 2018/11/12
     * @time 16:24
     * @title 活动宝箱纪录
     *
     */
    public function activityBoxLog()
    {
        return view();
    }

    /**
     * @return \think\response\Json
     * @user shuaishuaiwu
     * @date 2018/11/12
     * @time 16:26
     * @title 活动宝箱纪录数据
     *
     */
    public function activityBoxLogData()
    {
        $activityBoxLogModel = new ActivityBoxLogModel();
        $data = $activityBoxLogModel->getDataList();
        return json($data);
    }

    /**
     * @return \think\response\View
     * @user shuaishuaiwu
     * @date 2018/11/14
     * @time 10:54
     * @title 活动宝箱礼物列表
     */
    public function activityBoxIndex()
    {
        $giftModel = new GiftModel();
        $vehicleModel = new VehicleModel();
        $gift_list =  $giftModel->getDataList();
        $vehicle_list = $vehicleModel->getDataList();
        $this->assign(['gift_list'=>$gift_list,'vehicle_list'=>$vehicle_list]);
        return view();
    }

    /**
     * @return \think\response\Json
     * @user shuaishuaiwu
     * @date 2018/11/14
     * @time 14:53
     * @title 活动宝箱礼物列表数据
     */
    public function activityBoxIndexData()
    {
        $activityBoxPrizeModel = new ActivityBoxPrizeModel();
        $data = $activityBoxPrizeModel->getDataList();
        return json($data);
    }

    /**
     * @return \think\response\Redirect
     * @user 帅帅吴
     * @date 2018/11/14
     * @time 16:03
     * @title 活动宝箱礼物添加
     */
    public function activityBoxAdd()
    {
        if(request()->isPost()){
            $newData = input('post.');
            if(!empty($newData['typed_id'])){
                $newData['type_id'] = $newData['typed_id'];
                $newData['type'] = 2;
            }else
                $newData['type'] = 1;
            if(empty($newData['type_id']) || empty($newData['default_stock']) || empty($newData['status']))
                $this->error('请完整填写');
            try{
                $activityBoxPrizeModel = new ActivityBoxPrizeModel();
                $activityBoxPrizeModel->save($newData);
            }catch(\Exception $e){
                $this->error(lang('add').lang('false').$e->getMessage());
            }
            $this->success(lang('add').lang('success'));
        }
        return redirect('/Miss');
    }
    public function activityBoxEdit()
    {
        if(request()->isGet()){
            $newData['default_stock'] = input('get.default_stock');
            $newData['prize_stock'] = input('get.prize_stock');
            $newData['gift_notice'] = input('get.gift_notice');
            $newData['id'] = input('get.id');
            try{
                $activityBoxPrizeModel = new ActivityBoxPrizeModel();
                $activityBoxPrizeModel->save($newData,['id'=>$newData['id']]);
            }catch(\Exception $e){
                $this->error(lang('edit').lang('false').$e->getMessage());
            }
            $this->success(lang('edit').lang('success'));
        }
        return redirect('/Miss');
    }
}